package com.sumainfor.common.config;

import com.qiniu.common.Zone;
import com.sumainfor.common.util.HttpUtil;
import com.sumainfor.common.util.RemoteFileUpload;
import com.sumainfor.common.wechat.WxUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeansException;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;

/**
 * @Author PuaChen
 * @Create 2019-01-03 11:17
 */
@SpringBootConfiguration
@Log4j2
public class SpringContextConfiguration implements ApplicationContextAware {

    private static ApplicationContext APPLICATION_CONTEXT;

    @Bean
    public FilterRegistrationBean requestFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new RequestFilter());
        registration.addUrlPatterns("/*");
        registration.setName("RequestFilter");
        return registration;
    }

    @Bean
    public ServletListenerRegistrationBean<SessionListener> sessionListener() {
        ServletListenerRegistrationBean<SessionListener> bean = new ServletListenerRegistrationBean();
        bean.setListener(new SessionListener());
        return bean;
    }

    @Bean
    public HttpUtil createHttpUtil() {
        return new HttpUtil();
    }

    @Bean
    public RemoteFileUpload createRemoteFileUpload() {
        return new RemoteFileUpload(Zone.zone0());
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        APPLICATION_CONTEXT = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return APPLICATION_CONTEXT;
    }

    @Bean
    public WxUtil createWxUtil() {
        return new WxUtil();
    }

}
